package com.letian.web.controller.info;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.letian.common.constant.Constants;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.letian.common.annotation.Log;
import com.letian.common.core.controller.BaseController;
import com.letian.common.core.domain.AjaxResult;
import com.letian.common.enums.BusinessType;
import com.letian.info.domain.InfoUserFollow;
import com.letian.info.service.IInfoUserFollowService;
import com.letian.common.utils.poi.ExcelUtil;
import com.letian.common.core.page.TableDataInfo;

/**
 * 用户关注Controller
 * 
 * @author bmz
 * @date 2023-12-01
 */
@RestController
@RequestMapping("/info/follow")
public class InfoUserFollowController extends BaseController
{
    @Autowired
    private IInfoUserFollowService infoUserFollowService;

    /**
     * 查询用户关注列表
     */
    @PreAuthorize("@ss.hasPermi('info:follow:list')")
    @GetMapping("/list")
    public TableDataInfo list(InfoUserFollow infoUserFollow)
    {
        startPage();
        List<InfoUserFollow> list = infoUserFollowService.selectInfoUserFollowList(infoUserFollow);
        return getDataTable(list);
    }

    /**
     * 导出用户关注列表
     */
    @PreAuthorize("@ss.hasPermi('info:follow:export')")
    @Log(title = "用户关注", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InfoUserFollow infoUserFollow)
    {
        List<InfoUserFollow> list = infoUserFollowService.selectInfoUserFollowList(infoUserFollow);
        ExcelUtil<InfoUserFollow> util = new ExcelUtil<InfoUserFollow>(InfoUserFollow.class);
        util.exportExcel(response, list, "用户关注数据");
    }

    /**
     * 获取用户关注详细信息
     */
    @PreAuthorize("@ss.hasPermi('info:follow:query')")
    @GetMapping(value = "/{userFollowId}")
    public AjaxResult getInfo(@PathVariable("userFollowId") Long userFollowId)
    {
        return success(infoUserFollowService.selectInfoUserFollowByUserFollowId(userFollowId));
    }

    /**
     * 新增用户关注
     */
    @PreAuthorize("@ss.hasPermi('info:follow:add')")
    @Log(title = "用户关注", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InfoUserFollow infoUserFollow)
    {
        return toAjax(infoUserFollowService.insertInfoUserFollow(infoUserFollow));
    }

    @Log(title = "关注用户", businessType = BusinessType.INSERT)
    @GetMapping("/user/{userName}")
    public AjaxResult isFollowUser(@PathVariable("userName") String userName)
    {
        InfoUserFollow infoUserFollow = new InfoUserFollow();
        infoUserFollow.setUserName(getUsername());
        infoUserFollow.setFollowUserName(userName);
        List<InfoUserFollow> infoUserFollows = infoUserFollowService.selectInfoUserFollowList(infoUserFollow);
        if (infoUserFollows.size() > 0) {
            return success(Constants.SYS_YES);
        }
        return success(Constants.SYS_NO);
    }

    @Log(title = "关注用户", businessType = BusinessType.INSERT)
    @PostMapping("/user/{userName}")
    public AjaxResult followUser(@PathVariable("userName") String userName)
    {
        InfoUserFollow infoUserFollow = new InfoUserFollow();
        infoUserFollow.setUserName(getUsername());
        infoUserFollow.setFollowUserName(userName);
        return toAjax(infoUserFollowService.insertInfoUserFollow(infoUserFollow));
    }

    @Log(title = "取消关注用户", businessType = BusinessType.INSERT)
    @DeleteMapping("/user/{userName}")
    public AjaxResult unfollowUser(@PathVariable("userName") String userName)
    {
        InfoUserFollow infoUserFollow = new InfoUserFollow();
        infoUserFollow.setUserName(getUsername());
        infoUserFollow.setFollowUserName(userName);
        List<InfoUserFollow> infoUserFollows = infoUserFollowService.selectInfoUserFollowList(infoUserFollow);
        if (infoUserFollows.size() > 0) {
            InfoUserFollow insertModel = infoUserFollows.get(0);
            infoUserFollowService.deleteInfoUserFollowByUserFollowId(insertModel.getUserFollowId());
        }
        return success();
    }

    /**
     * 修改用户关注
     */
    @PreAuthorize("@ss.hasPermi('info:follow:edit')")
    @Log(title = "用户关注", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InfoUserFollow infoUserFollow)
    {
        return toAjax(infoUserFollowService.updateInfoUserFollow(infoUserFollow));
    }

    /**
     * 删除用户关注
     */
    @PreAuthorize("@ss.hasPermi('info:follow:remove')")
    @Log(title = "用户关注", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userFollowIds}")
    public AjaxResult remove(@PathVariable Long[] userFollowIds)
    {
        return toAjax(infoUserFollowService.deleteInfoUserFollowByUserFollowIds(userFollowIds));
    }
}
